/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.polyline;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import twaver.Element;
import twaver.PolyLine;
import twaver.Segment;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TerminalPoint;
import twaver.Node;
import twaver.network.TNetwork;
import twaver.TWaverUtil;

public class Network2 extends TNetwork{

	private PolyLine polyline = new PolyLine();

	public Network2(){
		this.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);
		this.createPolyLine();
		this.interaction();
	}

	public void interaction(){
		// load all nodes when the polyline is double clicked.
		this.addElementDoubleClickedActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Element element = (Element)e.getSource();
	    		if(element instanceof PolyLine){
	    			PolyLine line = (PolyLine)element;
	    			if(Boolean.FALSE.equals(line.getClientProperty("isAllNodeLoaded"))){
	    				List segments = line.getSegments();
	    				for(int i=0; i<segments.size()-1; i++){
	    					Segment segment = (Segment)segments.get(i);
	    					Node node = new TerminalPoint();
	    					node.setName("Node " + i);
	    					Rectangle bounds = node.getBounds();
	    					Point location = segment.getToPoint();
	    					node.setLocation(location.x-bounds.width/2, location.y-bounds.height/2);
	    					getDataBox().addElement(node);
	    					segment.setToNode(node);
	    					if(i < line.getSegments().size()-1){
	    						segment = (Segment)line.getSegments().get(i+1);
	    						segment.setFromNode(node);
	    					}
	    				}
	    				line.putClientProperty("isAllNodeLoaded", Boolean.TRUE);
	    			}
	    		}
			}
		});

	}

	private void createPolyLine(){
		polyline.setName("PolyLine2");

		TDataBox box = this.getDataBox();
		final Node fromNode = new TerminalPoint();
		fromNode.setLocation(30, 60);
		fromNode.setName("From Node");
		box.addElement(fromNode);

		final Node toNode = new TerminalPoint();
		toNode.setLocation(600, 60);
		toNode.setName("To Node");
		box.addElement(toNode);

        polyline.putLinkFlowing(true);
        polyline.putLinkFlowingColor(Color.RED.darker());
        polyline.putLinkColor(Color.GREEN);
        polyline.putLinkFlowingWidth(3);
        polyline.putLinkWidth(3);

        Point lastPoint = null;
		for(int i=1; i<=10; i++){
			Point point = new Point(30 + 50 * i, 50 + TWaverUtil.getRandomInt(40));
			Segment segment = null;
			if(lastPoint == null){
				segment = new Segment(fromNode, point);
			}else{
				segment = new Segment(lastPoint, point);
			}
			segment.putClientProperty("name", "Segment " + i);
			polyline.addSegment(segment);
			lastPoint = point;
		}
		Segment segment = new Segment(lastPoint, toNode);
		segment.putClientProperty("name", "LastSegment");
		polyline.addSegment(segment);
		polyline.putClientProperty("isAllNodeLoaded", Boolean.FALSE);
		box.addElement(polyline);
	}
}